package com.itheima.web;

import com.itheima.domain.Movies;
import com.itheima.domain.MoviesExample;
import com.itheima.service.MoviesService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

//@Controller
//@ResponseBody
@RestController
@RequestMapping("/movies")
public class MoviesController {

    @Autowired
    private MoviesService moviesService;

    @RequestMapping("/findList")
//    @ResponseBody
    public List<Movies> findList(Integer cid, String keyword) {
        MoviesExample moviesExample = new MoviesExample();
        MoviesExample.Criteria criteria = moviesExample.createCriteria();

//        如果cid和keyword为空就不构建条件了
        if (cid != null) {
            criteria.andCidEqualTo(cid);
        }
        if (StringUtils.isNotBlank(keyword)) {
            criteria.andKeywordLike("%" + keyword + "%");
        }

        List<Movies> moviesList = moviesService.findList(moviesExample);
        return moviesList;
    }

    @RequestMapping(value = "/save", name = "影视保存")
    public String save(@RequestBody Movies movies) {
        Integer id = movies.getId();
        if (id == null) {
            moviesService.insert(movies);
        } else {
            moviesService.update(movies);
        }

        return "OK";
    }

    @RequestMapping("/findById")
    public Movies findById(Integer id) {
        return moviesService.findById(id);

    }

    @RequestMapping("/deleteById")
    public String deleteById(Integer id) {
        moviesService.deleteById(id);
        return "OK";
    }

    @RequestMapping("/deleteByIds")
    public String deleteByIds(Integer[] ids) {
        moviesService.deleteByIds(ids);
        return "OK";
    }

}
